package cap.action;

import cap.dao.ChatDao;
import cap.dao.impl.ChatDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/exit")
public class exitServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf8");
        ChatDao chatDao = new ChatDaoImpl();
        HttpSession session = request.getSession();
        try{
            chatDao.exit(session.getAttribute("nickname").toString(), (String) session.getAttribute("room"));
        }catch (Exception e){
            e.printStackTrace();
        }
        session.setAttribute("room","");
        response.sendRedirect("front/chatRoom.jsp");
    }
}
